package com.cskaoyan.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author stone
 * @date 2022/03/21 14:46
 */
public class LifeCycleBean implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean, DisposableBean/*, BeanPostProcessor*/ {

    String username;

    String beanName;
    BeanFactory beanFactory;
    ApplicationContext applicationContext;

    public LifeCycleBean() {
        System.out.println("1 无参构造");
    }

    public void setUsername(String username) {
        this.username = username;
        System.out.println("2 设置参数：" + username);
    }

    //当前实例的成员变量的值来源于set方法的形参 → 形参的值都是由Spring提供的
    @Override
    public void setBeanName(String beanName) {
        this.beanName = beanName;
        System.out.println("BeanName");
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
        System.out.println("BeanFactory");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        System.out.println("ApplicationContext");
    }

    //需要告知这个组件init-method执行的是当前方法
    public void method1() {
        System.out.println("自定义init方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean的方法");
    }

    //需要告知这个组件destroy-method执行的是当前方法
    public void method2() {
        System.out.println("自定义destroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean提供的destroy");
    }
}
